﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Entities;
using Context;
namespace WCF_ListenMusic.Services.Module_Song.Admin
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ManageSongService" in code, svc and config file together.
    [ServiceBehavior(TransactionAutoCompleteOnSessionClose = true)]
    public class ManageSongService : IManageSongService
    {

        Context.Connect_DBDataContext context = new Context.Connect_DBDataContext();
        public List<SongSinger> manageActiveSong(int typeid)
        {
            try {
                var q = from a in context.SongSingers
                        where a.Active.Equals(false) && a.TypeID.Equals(typeid)
                        select a;
                return q.ToList();
            }
            catch (Exception e) { throw new FaultException (e.Message);}
        }


        public bool activeSong(int songid)
        {
            try
            {
                var q = from a in context.Songs
                        where a.SongID.Equals(songid)
                        select a;
                Song s = q.Single();
                s.Active = true;
                context.SubmitChanges();
                return true;
            }
            catch (Exception e) { return false; throw new FaultException(e.Message); }
        }


        public List<SongSinger> getAllSongSinger(int typeid)
        {
            try
            {
                var q = from a in context.SongSingers
                        where a.TypeID.Equals(typeid)
                        select a;
                return q.ToList();
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }


        public List<SongSinger> getSongSinger_bySongName(string songName, int typeid)
        {
            try
            {
                var q = from a in context.SongSingers
                        where a.TypeID.Equals(typeid) && a.SongName.Trim().ToLower().Contains(songName.Trim().ToLower())
                        select a;
                return q.ToList();
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }


        public bool insertNewSong(Song song)
        {            
            try
            {   
                context.Songs.InsertOnSubmit(song);
                context.SubmitChanges();
                return true;
            }
            catch (Exception e) { return false; throw new FaultException(e.Message); }
        }


        public int getMax_SongID()
        {
            try
            {
                var q = (from a in context.Songs
                        select a.SongID).Max();
                return q;
            }
            catch (Exception e) {throw new FaultException(e.Message); }
        }


        public bool deleteSong_bySongID(int songid)
        {
            try
            {
                var q = from a in context.Songs
                        where a.SongID.Equals(songid)
                        select a;
                Song s = q.Single();
                context.Songs.DeleteOnSubmit(s);
                context.SubmitChanges();
                return true;
            }
            catch (Exception e) { return false; throw new FaultException(e.Message); }
        }

        [OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]
        public bool updateSong(Song s)
        {
            context.CommandTimeout = 3600;
            try
            {
                var q = from a in context.Songs
                        where a.SongID.Equals(s.SongID)
                        select a;
                Song sOld = q.Single();
                sOld.SongName = s.SongName;
                sOld.SingerID = s.SingerID;
                sOld.SingerID2 = s.SingerID2;
                sOld.TypeID = s.TypeID;
                sOld.MusicianID = s.MusicianID;
                sOld.Lyrics = s.Lyrics;
                sOld.CountryID = s.CountryID;
                sOld.CategoryID = s.CategoryID;
                sOld.SongURL = s.SongURL;
                sOld.Active = s.Active;
                context.SubmitChanges();
                return true;
            }
            catch (Exception e) { return false; throw new FaultException(e.Message); }
        }


        public SongSinger getSongSinger_bySongID(int songid)
        {
            try
            {
                var q = from a in context.SongSingers
                        where a.SongID.Equals(songid)
                        select a;
                return q.SingleOrDefault();
            }
            catch (Exception e) {  throw new FaultException(e.Message); }
        }


        public List<SongAlbum> getSongAlbum_byAlbumID(int albumid)
        {
            try
            {
                var q = from a in context.SongAlbums
                        where a.AlbumID.Equals(albumid)
                        select a;
                return q.ToList();
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }
    }
}
